﻿// ***********************************************************************
// Assembly         : TfsDispatcher
// Author           : Eric Cocquerez
// Created          : 01-27-2015
//
// Last Modified By : Eric Cocquerez
// Last Modified On : 01-27-2015
// ***********************************************************************
// <copyright file="GetConfiguration.cs" company="Personnel">
//     Copyright (c) Personnel. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TfsDispatcher
{
	using System.Diagnostics;
	using System.Xml.Linq;

	/// <summary>
	/// Class GetConfiguration.
	/// </summary>
	public static class GetConfiguration
	{
		/// <summary>
		/// Initializes static members of the <see cref="GetConfiguration"/> class.
		/// </summary>
		static GetConfiguration()
		{
			MulticastAddress = "224.34.1.1";
			MulticastPort = "4151";
			IsDebug = false;
			IsLog = false;
			ConfigFileName = AppDomain.CurrentDomain.BaseDirectory + "bin\\Plugins\\TfsDispatcher.dll.config"; ;
		}

		#region Properties
		/// <summary>
		/// Gets or sets the multicast address used for MSMQ
		/// </summary>
		/// <value>The multicast address.</value>
		public static string MulticastAddress { get; set; }

		/// <summary>
		/// Gets or sets the multicast port used for MSMQ.
		/// </summary>
		/// <value>The multicast port.</value>
		public static string MulticastPort { get; set; }

		/// <summary>
		/// Gets or sets a value indicating whether this instance is debug (Warning, launch a debuger when receiving a new BuildQualityChangedEvent)
		/// </summary>
		/// <value><c>true</c> if this instance is debug; otherwise, <c>false</c>.</value>
		public static bool IsDebug { get; set; }

		/// <summary>
		/// Gets or sets a value indicating whether there is a lot of log.
		/// </summary>
		/// <value><c>true</c> if this instance is log; otherwise, <c>false</c>.</value>
		public static bool IsLog { get; set; }

		/// <summary>
		/// Gets or sets the name of the configuration file.
		/// </summary>
		/// <value>The name of the configuration file.</value>
		public static string ConfigFileName { get; set; }
		#endregion

		/// <summary>
		/// Initializes the configuration.
		/// </summary>
		public static void InitializeConfiguration()
		{
			try
			{
				// Read config file as xml file
				var xmlDoc = XDocument.Load(ConfigFileName);
				var result =
					xmlDoc.Descendants("appSettings")
						.Elements("add")
						.Where(c => c.Attribute("key").Value == "IsDebug")
						.Select(c => c.Attribute("value"));
				foreach (var attribute in result.Where(attrib => attrib != null))
				{
					IsDebug = Convert.ToBoolean(attribute.Value);
					break;
				}

				result =
					xmlDoc.Descendants("appSettings")
						.Elements("add")
						.Where(c => c.Attribute("key").Value == "IsLog")
						.Select(c => c.Attribute("value"));
				foreach (var attribute in result.Where(attrib => attrib != null))
				{
					IsLog = Convert.ToBoolean(attribute.Value);
					break;
				}

				result =
					xmlDoc.Descendants("appSettings")
						.Elements("add")
						.Where(c => c.Attribute("key").Value == "MulticastAddress")
						.Select(c => c.Attribute("value"));
				foreach (var attribute in result.Where(attrib => attrib != null))
				{
					MulticastAddress = attribute.Value;
					break;
				}
				result =
					xmlDoc.Descendants("appSettings")
						.Elements("add")
						.Where(c => c.Attribute("key").Value == "MulticastPort")
						.Select(c => c.Attribute("value"));
				foreach (var attribute in result.Where(attrib => attrib != null))
				{
					MulticastPort = attribute.Value;
					break;
				}
			}
			catch (Exception exception)
			{
				var appLog = new EventLog("TfsDispatcher");
				appLog.Source = "Application";
				appLog.Log = "Application";
				appLog.WriteEntry("Exception while reading config file : " + exception.Message);
			}
		}
	}
}
